﻿using Rbac.Domain.RoleMenus;
using Rbac.Repository.Menus;
using Rbac.Repository.RoleMenus;
using Rbac.Repository.Roles;
using Rbac.Services.Roles.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace Rbac.Services.Roles
{
    public class RoleService : IRoleMenuService
    {
        private readonly IRoleRepository repository;
        private readonly IMenuRepository menuRepository;
        private readonly IRoleMenuRepository roleMenuRepository;

        public RoleService(IRoleRepository repository, IMenuRepository menuRepository, IRoleMenuRepository roleMenuRepository)
        {
            this.repository = repository;
            this.menuRepository = menuRepository;
            this.roleMenuRepository = roleMenuRepository;
        }

        public List<RoleDto> Query()
        {
            var list = repository.QueryAll().Select(m => new RoleDto
            {
                CreaterId = m.CreaterId,
                CreateTime = m.CreateTime,
                RoleId = m.RoleId,
                RoleName = m.RoleName
            }).ToList();

            /*var permission = menuRepository.Query().Join(roleMenuRepository.Query(), a => a.MenuId, b => b.MenuId,
                (a, b) => new {
                    a.MenuName, b.RoleId
                }).ToList();

            list.ForEach(m => {
                m.Permission = string.Join(",", permission.Where(p => p.RoleId == m.RoleId).Select(m => m.MenuName));
            });*/

            return list;
        }

        public List<RoleMenuPermissionDto> QueryPermission()
        {
            return  menuRepository.Query().Join(roleMenuRepository.Query(), a => a.MenuId, b => b.MenuId,
                (a, b) => new RoleMenuPermissionDto{
                   MenuName =  a.MenuName,
                   RoleId =  b.RoleId
                }).ToList();
        }

        public List<int> QueryByRole(int RoleId)
        {
            return roleMenuRepository.Query().Where(m => m.RoleId == RoleId).Select(m => m.MenuId).ToList();
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <returns></returns>
        public ResultDto AssignPermission(PermissionDto permission)
        {
            //删除当前角色ID对应的记录
            var rolemenus = roleMenuRepository.Query().Where(m => m.RoleId == permission.RoleId).ToList();
            roleMenuRepository.ManyDelete(rolemenus);

            //第1种
            /*foreach (var item in permission.MenuId)
            {
                roleMenuRepository.Create(new RoleMenu
                {
                    MenuId = item,
                    RoleId = permission.RoleId
                });
            }*/

            //第2种：
            var entitys = permission.MenuId.Select(m => new RoleMenu
            {
                MenuId = m,
                RoleId = permission.RoleId
            }).ToList();

            roleMenuRepository.Create(entitys);

            return new ResultDto {  Code = State.Success};
        }
    }
}
